Logical OR (||)

While using || (logical OR), we must put the condition first whose probability of getting true is high so that compiler doesn’t need to check the second condition if the first condition is true.

C++




// C++ program for the above approach
 
#include <iostream>
 
using namespace std;
 
// Function to check whether n is odd
bool isEven(int n) {
    return (n % 2 == 0);
}
 
// Function to check whether n is prime
bool isPrime(int n) {
    if (n <= 1)
        return false;
    for (int i = 2; i <= n / 2; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}
 
int main() {
    int cnt = 0, n = 10;
 
    // Implementation 1
    for (int i = 3; i <= n; i++) {
        if (isEven(i) || !isPrime(i))
            cnt++;
    }
 
    return 0;
}
 
// This code is contributed by codebraxnzt


C




#include <stdio.h>
 
// Function to check whether n is odd
bool isEven(int n);
 
// Function to check whether n is prime
bool isPrime(int n);
 
int main()
{
    int cnt = 0, n = 10;
 
    // Implementation 1
    for (int i = 3; i <= n; i++) {
        if (isEven(i) || !isPrime(i))
            cnt++;
    }
}


Java




import java.io.*;
 
class GFG {
// Function to check whether n is odd
boolean isEven(int n);
  
// Function to check whether n is prime
boolean isPrime(int n);
  
public static void main (String[] args)
{
    int cnt = 0, n = 10;
  
    // Implementation 1
    for (int i = 3; i <= n; i++) {
        if (isEven(i) || !isPrime(i))
            cnt++;
    }
}
}
 
// This code is contributed by Utkarsh


Python3




# Function to check whether n is even
def isEven(n):
    pass
 
# Function to check whether n is prime
def isPrime(n):
    pass
 
if __name__ == '__main__':
    cnt = 0; n = 10
 
    # Implementation 1
    for i in range(3,n) :
        if isOdd(i) or not isPrime(i):
            cnt+=1


C#




using System;
 
class GFG {
// Function to check whether n is odd
bool isEven(int n)
{
return (n % 2 == 0);
}
  // Function to check whether n is prime
bool isPrime(int n)
{
    if (n <= 1)
        return false;
 
    for (int i = 2; i <= Math.Sqrt(n); i++) {
        if (n % i == 0)
            return false;
    }
 
    return true;
}
 
static void Main(string[] args)
{
    int cnt = 0, n = 10;
 
    // Implementation 1
    for (int i = 3; i <= n; i++) {
        if (new GFG().isEven(i) || !new GFG().isPrime(i))
            cnt++;
    }
}
}


Javascript




// Function to check whether n is odd
function isEven(n);
 
// Function to check whether n is prime
function isPrime(n);
 
 
    let cnt = 0, n = 10;
 
    // Implementation 1
    for (let i = 3; i <= n; i++) {
        if (isEven(i) || !isPrime(i))
            cnt++;
    }
 
// This code is contributed by Aman Kumar


As described earlier that the probability of a number being even is more than that of it being a non-prime. The current order of execution of the statements doesn’t allow even numbers greater than 2 to be checked whether they are non-prime (as they are all non-primes). 

Note: For larger inputs, the order of the execution of statements can affect the overall execution time for the program.
 



Code Optimization Technique (logical AND and logical OR)

Similar Reads

Logical AND (&&)

While using && (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false....

Logical OR (||)

...

Contact Us